package Deom1;

import Deom1.DBUtils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Deom3_insert {
    public static void main(String[] args) {
        Connection connection=null;
        PreparedStatement statement=null;

        try {
            //获取数据库连接
            connection=DBUtil.getConnection();
            String sql="insert into student(id,name,class_id)values(?,?,?)";
//定义sql预处理对象
            statement=connection.prepareStatement(sql);
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入学生id：");
            int id =scanner.nextInt();

            System.out.println("请输入学生姓名：");
            String name=scanner.next();

            System.out.println("请输入班级编号：");
            int class_id=scanner.nextInt();

            statement.setInt(1,id);
            statement.setString(2,name);
            statement.setInt(3,class_id);

            int row= statement.executeUpdate();
            if (row==1){
                System.out.println("插入成功");
            }else {
                System.out.println("插入失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(null,statement,connection);
        }
    }
}
